package com.tianpengtech.admin.fair;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.DateUtils;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBetween;
import com.tianpengtech.common.validator.rules.other.MustBeNull;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;
import com.tianpengtech.common.validator.rules.string.StringMustBeMobile;
import com.tianpengtech.common.validator.rules.string.StringMustBeRequired;
import com.tianpengtech.front.web.fair.FairService;

public class FairAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

		
		Fair fair = new FairService().findByMobile(model.getStr(Fair.MOIBILE));
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Fair.NICK_NAME), "昵称只能是1-15个字符", 1, 15));
//		validator.addRule(new StringLengthBetween(model.getStr(Fair.ID_CARD), "请输入正确的身份证号码", 1,18));
		validator.addRule(new StringMustBeMobile(model.getStr(Fair.MOIBILE), "请输入正确的手机号码格式"));
		validator.addRule(new MustBeNull(fair, "当前手机号已经存在"));
//		validator.addRule(new NumbericMustBetween(model.getInt(Fair.AGE), "请输入正确的年龄范围（1-99）", 1, 99));
//		validator.addRule(new StringMustBeRequired(model.getStr(Fair.HEIGHT), "请输入身高"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.PROVINCE), "请输入所在地区"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.CITY), "请输入所在市区"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.CHANNEL),"请输入推广渠道"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.PLATFORM), "请输入平台"));
		validator.excute();
		
		model.set(Fair.STATUS,Fair.STATUS_SUCCESS);
		PushKit.pushSystemMessageNow("管理员【通过】"+model.getStr(Fair.NICK_NAME)+"的认证请求");
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {
		
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Fair.NICK_NAME), "昵称只能是1-15个字符", 1, 15));
		validator.addRule(new StringMustBeMobile(model.getStr(Fair.MOIBILE), "请输入正确的手机号码格式"));
//		validator.addRule(new NumbericMustBetween(model.getInt(Fair.AGE), "请输入正确的年龄范围（1-99）", 1, 99));
//		validator.addRule(new StringMustBeRequired(model.getStr(Fair.HEIGHT), "请输入身高"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.PROVINCE), "请输入所在地区"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.CITY), "请输入所在市区"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.CHANNEL),"请输入推广渠道"));
		validator.addRule(new StringMustBeRequired(model.getStr(Fair.PLATFORM), "请输入平台"));
		validator.excute();
	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		String nickName = model.getStr(Fair.NICK_NAME);
		queryConstruct
			.and(new EqualCondition(Fair.DELETETAG, Fair.DELETE_STATUS_NO));
		if(StringUtils.isNotEmpty(nickName)){
			queryConstruct
			.and(new LikeCondition(Fair.NICK_NAME,"%"+nickName+"%"));
		}
	}

	
	@Override
	public boolean deleteById(Model<?> model, int id) throws ValidateException, ServiceException {
		model.set(Fair.DELETETAG, Fair.DELETE_STATUS_YES);
		return model.update();
	}
	
	@Override
	public String tableName() {
		return Fair.dao.tableName();
	}

	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {
		
		String content = params.getStr(Opertion.CONTENT);
		Validator validator = new Validator();
		if(status==Fair.STATUS_FAILURE){
			validator.addRule( new StringLengthBetween(content,"拒绝理由必须是1-40个字符之间",1,40));
		}
		validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0")).excute();
		model.set(Fair.ID, id);
		model.set(Fair.STATUS, status);
		model.update();
		Model<Fair> temp = Fair.dao.findById(id);
		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		if(status==Fair.STATUS_SUCCESS){
			String result = "【通过】"+temp.getStr(Fair.NICK_NAME)+"的认证请求";
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_LOG,userId,result);
		}
		if(status==Fair.STATUS_FAILURE){
			String result = "【拒绝】"+temp.getStr(Fair.NICK_NAME)+"的认证请求,原因是:"+content;
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_LOG,userId,result);
		}
	}

	
	
}